﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class StudentRegistration : System.Web.UI.Page
{
    SendEmails auto = new SendEmails();
    RegistrationData reg = new RegistrationData();
    BusinessLogic busin = new BusinessLogic();
    string salt = string.Empty, Password = string.Empty;
    DataSet ds = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //divMessage.Visible = false;
            //divRegistration.Visible = true;
            txtEmail.Text = "eg : john.smith@gmail.com";
            txtFirstName.Text = "eg : John";
            txtLastName.Text = "eg : Smith";
        }
        if (Session["User"] != null)
        {
            Response.Redirect("~/Reg-Update-Profile.aspx");
        }
        else
        {
            busin.checkcookies_ForLogin(Request.Url.ToString());
        }
    }

    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        ds = new DataSet();
        ValidationSummary1.Visible = false;

        //CaptchaControl1.ValidateCaptcha(txtCapcha.Text);
        if (ValidatePage())
        {
            //if (CaptchaControl1.UserValidated)
            //{
            salt = reg.RandomString(5);
            Password = reg.protectpassword(reg.protectpassword(txtPassword.Text) + salt);
            reg.FirstName = txtFirstName.Text;
            reg.LastName = txtLastName.Text;
            //  reg.DOB = txtDOB.Text;
            //reg.Address = txtAddress.Text;
            //reg.UserName = txtUsername.Text;
            reg.Email = txtEmail.Text;
            reg.Password = Password;
            reg.Salt = salt;
            ds = reg.InsertRegistrationDetails();
            if (ds != null)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                //    EmailData.UserName = txtUsername.Text;
                    EmailData.Name = txtFirstName.Text;

                    string strConfig = string.Empty;
                    strConfig = EmailData.WebsiteLink;
                    string Link = strConfig + "Reg-ActivateAccount.aspx?id=";
                    string strCode = DateTime.Now.ToString("MM/dd/yyyyhh:mm:ss:fff");
                    strCode = rv.RemoveSpecialChar1(strCode);
                    strCode = strCode + "-" + Convert.ToString(dr["UserID"]);
                    Link = Link + strCode;
                    reg.Update_EmailVerificationLink(Convert.ToInt32(dr["UserID"]), Link);
                    string subject = EmailData.WebsiteAdmin + " - Activate Account";
                    EmailData.Link = Link;
                    try
                    {
                        auto.GeneralEmail(subject, txtEmail.Text, 1);
                        lblError.Text = "Thanks for registration with us. We have sent you an email please verify your email.";
                    }
                    catch (Exception ex)
                    {
                        lblError.Text = ex.Message;
                    }
                    reset();
                    //divMessage.Visible = true;
                    //divRegistration.Visible = false;
                }
                else lblError.Text = "Some Error has occured, please try again.";
            }
            else lblError.Text = "Some Error has occured, please try again.";
            //}
            //else
            //    ValidationSummary1.Visible = true;
        }
    }

    #region for Validation

    protected bool ValidatePage()
    {
        bool IsValidated = true;
        if (MandatoryFieldsCheck())
        {
            if (!PatternCheck())
            {
                IsValidated = false;
            }
        }
        else
        {
            IsValidated = false;
        }
        return IsValidated;
    }

    protected bool MandatoryFieldsCheck()
    {
        lblError.Text = string.Empty;

        if (txtFirstName.Text == string.Empty)
        {
            lblError.Text = "Required field - First Name";
            return false;
        }
        //if (txtLastName.Text == string.Empty)
        //{
        //    lblError.Text = "Required field - Last Name";
        //    return false;
        //}
        //if (txtAddress.Text == string.Empty)
        //{
        //    lblError.Text = "Required field - Address";
        //    return false;
        //} 
        if (txtEmail.Text == string.Empty)
        {
            lblError.Text = "Required field - Email";
            return false;
        }
        Validations vald = new Validations(); 
        if (Convert.ToBoolean(vald.CheckEmail(txtEmail.Text)))
        {
            lblError.Text = "This Email already exists";
            return false;
        } 
        if (txtPassword.Text == string.Empty)
        {
            lblError.Text = "Required field - Password";
            return false;
        }
        if (txtConfirmPassword.Text == string.Empty)
        {
            lblError.Text = "Required field - Confirm Password";
            return false;
        }

        if (txtPassword.Text != txtConfirmPassword.Text)
        {
            lblError.Text = "Password and confirm password do not match.";
            return false;
        }

        //DateTime result;
        //if (!DateTime.TryParse(txtDOB.Text,out result))
        //{

        //    lblError.Text = "Please enter valid date of birth.";
        //    return false;
        //}
        //if (Convert.ToDateTime(txtDOB.Text) > DateTime.Now)
        //{
        //    lblError.Text = "Your date of birth can not be greater than today";
        //    return false;
        //}
        //if (txtlname.Text == string.Empty)
        //    lblError.Text = "Required field -Last Name";
        //if (txtname.Text == string.Empty)
        //    lblError.Text = "Required field -First Name";

        return true;
    }
    RegexValidator rv = new RegexValidator();

    protected bool PatternCheck()
    {
        lblError.Text = string.Empty;

        RegularExpressionValidator re = new RegularExpressionValidator();
        //int age = DateTime.Now.Year - Convert.ToDateTime(txtDOB.Text).Year;
        // if (age < 17 )
        // {
        //     lblError.Text = "Age should be greater than 18";
        //     return false;
        // } 
        if (!rv.IsRegularExpression_Email(txtEmail.Text.Trim()))
            lblError.Text = "E-MailID should be in correct format";

        if (!rv.IsValidatePwdFixedLength(txtPassword.Text.Trim()))
        {
            lblError.Text = "Password should be atleast 6 characters long";
            return false;
        }

        //if (txtPassword.Text != txtConfirmPassword.Text)
        //{
        //    lblError.Text = "Password do not match";
        //}

        //if (lblError.Text != "")
        //{
        //    IsPatterOK = false;
        //}
        return true;
    }

    void reset()
    {
        txtFirstName.Text = "";
        txtLastName.Text = "";
        // txtDOB.Text = ""; 
        txtEmail.Text = ""; 
        txtPassword.Text = "";
        txtConfirmPassword.Text = "";
        //txtCapcha.Text = "";
    }

    #endregion
}